<template>
  <div>
    <!-- 信用统计报表 -->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @resetChange="resetChange" @onCollapse="collapseHandle" :labelWidth="110"></TopFilter>

    <Spin :spinning="exportLoading" tip="Loading...">
      <FilterTable
        ref="table"
        columnsRef="qjhCreditStatisticsReport"
        :isSelectColumn="false"
        :columns="columns"
        :fetchapi="fetchApi"
        :params="fetchParams"
        :datakey="'records'"
        :onColumnsChange="columns => (this.columns = columns)"
      >
        <template slot="controls">
          <multiuse-button size="small" type="primary" :loading="exportLoading" :click="exportHandle">导 出</multiuse-button>
        </template>
      </FilterTable>
    </Spin>

    <!-- 经销商查询 -->
    <BaseDialog :visible.sync="isDealer" title="经销商查询" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <OwnerSearch :rowData="formData" @close="emitCloseHandler" />
    </BaseDialog>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { queryList, exportExcel,exporturl } from '@/api/omFinance/CRReportManage/CreditStatisticsReport';
import { exportExcelFile } from '@/utils/index';
import OwnerSearch from './OwnerSearch';

export default {
  name: 'CreditStatisticsReport',
  mixins: [dictionary],

  components: {
    OwnerSearch
  },
  data() {
    this.BaseTable = null;
    this.conf = { add: '新增', edit: '编辑', show: '明细' };

    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchApi: queryList,
      fetchParams: {},
      formData: {},
      isDisabled: true,
      visible: false,
      isDealer: false,
      exportLoading: false,
      handleType: '',
      drawerTitle: ''
    };
  },

  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  methods: {
    seacherFun() {
      this.isDealer = true;
    },
    emitCloseHandler(val, data) {
      this.isDealer = val;
      let dealerCodes = [];
      let dealerNames = [];
      data.map(x => {
        dealerCodes.push(x.dealerCode);
        dealerNames.push(x.dealerName);
      });
      this.topFilterList.find(x => x.fieldName == 'dealerCodes').initialValue = dealerCodes.toString();
    },
    changeHandle(params) {
      if (params.yearAndMonth) {
        params.reportYear = params.yearAndMonth.split('-')[0] || '';
        params.reportMonth = params.yearAndMonth.split('-')[1] || '';
      }

      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    resetChange(params) {
      params.reportYear = '';
      params.reportMonth = '';
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    emitCloseHandler1(val) {
      this.visible = val;
    },
    detailHandle(row) {
      this.handleType = 'show';
      this.formData = row;
      this.showHandler();
    },
    showHandler() {
      this.drawerTitle = `${this.conf[this.handleType]}`;
      this.visible = true;
    },

    // 导出
    async exportHandle() {
      let json = Object.assign({}, { CODE: 'CODE_0000073' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }
      // await this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
      //   confirmButtonText: '下载',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // })
      //   .then(() => {
      //     this.exportLoading = true;

      //     exportExcel(this.fetchParams)
      //       .then(response => {
      //         exportExcelFile(response.data, '信用统计报表');
      //         this.exportLoading = false;
      //       })
      //       .catch(err => {
      //         console.log(err);
      //       });
      //   })
      //   .catch(() => {
      //     this.$message({
      //       type: 'info',
      //       message: '已取消'
      //     });
      //   });
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCodes',
          placeholder: '请选择',
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun()}></el-button>
              </div>
            );
          }
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          placeholder: '请输入'
        },
        {
          type: 'DATE',
          label: '月度',
          fieldName: 'yearAndMonth',
          dateType: 'month',
          placeholder: '请选择'
        },
        {
          type: 'SELECT',
          label: '经销商形式',
          fieldName: 'dealerForm',
          itemList: this.createDictList(6092).filter(x => x.value !== 60921003)
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        // {
        //   title: '操作',
        //   dataIndex: 'column-action',
        //   fixed: 'left',
        //   render: (props, h) => {
        //     return (
        //       <div>
        //         <multiuse-button size="mini" type="text" onClick={() => this.detailHandle(props.row)}>
        //           明细
        //         </multiuse-button>
        //       </div>
        //     );
        //   }
        // },
        {
          title: '提报状态',
          dataIndex: 'submitOrderStatus',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商形式',
          dataIndex: 'dealerForm',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6092'),
          render: props => {
            return <span>{this.createDictText(props.row.dealerForm, '6092')}</span>;
          }
        },
        {
          title: '月度',
          dataIndex: 'yearAndMonth',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商代码',
          dataIndex: 'dealerCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '货币资金',
          dataIndex: 'qmhbzj',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          numberFormat: true
        },
        {
          title: '其他应收款',
          dataIndex: 'qmqtysk',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '预付账款',
          dataIndex: 'qmyfzk',
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '期末应收账款',
          dataIndex: 'qmyszk',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '年初应收账款',
          dataIndex: 'ncyszk',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '期末存货',
          dataIndex: 'qmch',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '年初存货',
          dataIndex: 'ncch',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '流动资产',
          dataIndex: 'qmldzchj',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          numberFormat: true
        },
        {
          title: '固定资产',
          dataIndex: 'qmgdzchj',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '资产总计',
          dataIndex: 'qmzczj',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '短期借款',
          dataIndex: 'qmdqjk',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '应付票据',
          dataIndex: 'qmyfpj',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '应付账款',
          dataIndex: 'qmyifzk',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '其他应付款',
          dataIndex: 'qmqtyfk',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '其他流动负债',
          dataIndex: 'qmqtldfz',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '负债合计',
          dataIndex: 'qmldfzhj',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '实收资本',
          dataIndex: 'qmsszb',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '未分配利润',
          dataIndex: 'qmwfplr',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '所有者权益',
          dataIndex: 'qmsyzqyhj',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '主营业务收入',
          dataIndex: 'bnzyywsr',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '主营业务成本',
          dataIndex: 'bnzyywcb',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '其他业务利润',
          dataIndex: 'bnqtywlr',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '财务费用',
          dataIndex: 'bncwfy',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '管理费用',
          dataIndex: 'bnglfy',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '销售费用',
          dataIndex: 'bnxsfy',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '净利润',
          dataIndex: 'bnjlr',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '现金流量净额',
          dataIndex: 'bnqmxjjxjdjwye',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经营性现金流入',
          dataIndex: 'bnjyhdxjlrxj',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经营性现金流出',
          dataIndex: 'bnjyhdxjlcxj',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经营性现金流量净额',
          dataIndex: 'bnjyhdcsdxjllje',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '投资性现金流量净额',
          dataIndex: 'bntzhdcsdxjllje',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '筹资性现金流量净额',
          dataIndex: 'bnczhdcsdxjllje',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '征税销售额（本年累计）',
          dataIndex: 'bnlrze',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    }
  }
};
</script>
